package com.itic.mall.system.users.shareLog.service;

import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.type.UserShareType;
import com.itic.mall.system.inter.users.shareLog.persistence.dao.UsersShareLogDao;
import com.itic.mall.system.inter.users.shareLog.persistence.model.UsersShareLog;
import com.itic.mall.system.inter.users.shareLog.persistence.model.UsersShareLogExample;
import com.itic.mall.system.inter.users.usersmall.persistence.model.UsersMall;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Service
@Transactional
public class UsersShareLogService
     extends CrudService<UsersShareLogDao,UsersShareLog,UsersShareLogExample> {
    @Autowired
    private UsersShareLogDao usersShareLogDao;

    /**
     * 添加用户分享记录
     * @param type 分享类型
     * @param goodId 如果是分享的是商品 添加商品Id
     * @param usersMall 用户
     * @return
     */
    public String addShareLog(UserShareType type,String goodId,UsersMall usersMall) {
    	UsersShareLog usersShareLog = new UsersShareLog();
    	usersShareLog.setGoodId(goodId);
    	usersShareLog.setUserId(usersMall.getId());
    	usersShareLog.setType(type.getType());
    	String result = this.add(usersShareLog);
    	return result;
    }
    /***
     * 获取最近几天用户分享的次数
     * @param userId 用户Id
     * @param date 截止到之前多久
     * @return
     */
    public int getUserShareCount(String userId,Date date) {
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put("userId",userId);
    	params.put("date", date);
    	Integer count = this.usersShareLogDao.getUserShareCount(params);
    	return count ==null?0:count.intValue();
    	
    }
    
}